import router, { varRoutes } from '@/router'
import { constantRoutes } from '@/router'

const state = {
  routes: [],
  point: {
    userAdd: 'POINT-USER-ADD',
    userUpdate: 'POINT-USER-UPDATE',
    userZz: 'POINT-USER-ZZ',
    userDel: 'POINT-USER-DELETE',
    rolesDel: 'POINT-USER-DELETE'
  }
}
const mutations = {
  setRoutes(state, payload) {
    state.routes = [...constantRoutes, ...payload]
  }
}
const actions = {
  filterRouter(context, payload) {
    const filterRouter = varRoutes.filter((item) =>
      payload.menus.includes(item.name)
    )
    // console.log(filterRouter)
    // 本来应该拥有权限的页面出现了404，这是因为404的匹配权限放在了静态路由，而动态路由在没有addRoutes之前，找不到对应的地址，就会显示404，所以我们需要将404放置到动态路由的最后
    // 把筛选出来的动态路由规则添加到路由规则中
    router.addRoutes([
      ...filterRouter,
      { path: '*', redirect: '/404', hidden: true }
    ])
    context.commit('setRoutes', filterRouter)
  }
}
const getters = {}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
